﻿using MS.Entities;
using MS.Enums.Common;
using MS.Utility.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace MS.Logic
{
    public partial class MS_Base_RoleLogic
    {
        /// <summary>
        /// 保存角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public int Save(int id,string name,long uid)
        {
            if (id == 0)
            {
                MS_Base_Role role = new MS_Base_Role();
                int seqid = 1;
                int count = this.GetCountByQuery(x=>true);
                seqid = count + 1;
                role.Name = name.Trim();
                role.SeqId = seqid;
                role.CreateTime = DateTimeHelper.GetTime(DateTime.Now);
                role.CreateUid = uid;
                role.UpdateTime = DateTimeHelper.GetTime(DateTime.Now);
                role.UpdateUid = uid;
                this.Add(role);
                id = role.Id;
            }
            else
            {
                MS_Base_Role role = this.GetById(id);
                if (role != null)
                {
                    role.Name = name;
                    role.UpdateTime = DateTimeHelper.GetTime(DateTime.Now);
                    role.UpdateUid = uid;
                    this.Save(role);
                }
            }
            return id;
        }

        /// <summary>
        /// 移动菜单或功能
        /// </summary>
        /// <param name="id">菜单或功能id</param>
        /// <param name="way">移动的方向</param>
        /// <param name="uid">操作人id</param>
        /// <returns></returns>
        public bool Move(int id, MoveWay way, long uid)
        {
            bool b = true;
            MS_Base_Role role = this.GetItemByQuery(x => x.Id == id && x.Status == (int)Status.Normal);
            if (role == null)
            {
                return false;
            }
            List<MS_Base_Role> list = null;
            MS_Base_Role tmpRole = null;
            switch (way)
            {
                case MoveWay.Up:
                    //向上移动
                    //查询同类集合
                    list = this.GetListByQuery(x => x.Status == (int)Status.Normal, "SeqId desc", false);
                    tmpRole = list.Find(x => x.SeqId < role.SeqId);
                    break;
                case MoveWay.Down:
                    //向下移动
                    //查询同类集合
                    list = this.GetListByQuery(x => x.Status == (int)Status.Normal , "SeqId", false);
                    tmpRole = list.Find(x => x.SeqId > role.SeqId);
                    break;
                default:
                    break;
            }
            if (tmpRole != null)
            {
                int tmpSeqId = tmpRole.SeqId;
                tmpRole.SeqId = role.SeqId;
                tmpRole.UpdateUid = uid;
                tmpRole.UpdateTime = DateTimeHelper.GetTime(DateTime.Now);

                role.SeqId = tmpSeqId;
                role.UpdateUid = uid;
                role.UpdateTime = DateTimeHelper.GetTime(DateTime.Now);
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                {
                    try
                    {
                        this.Save(tmpRole);
                        this.Save(role);
                        scope.Complete();
                    }
                    catch
                    {
                        throw;
                    }
                }

            }
            else
            {
                b = false;
            }
            return b;
        }
    }
}
